package com.shop.front.service.user;

import com.shop.common.enums.TrueFalseEnum;
import com.shop.domain.user.UserAddress;
import com.shop.front.dao.user.UserAddressDao;
import com.shop.front.dao.user.UserAddressSQLDao;
import com.shop.front.request.user.UserAddressReq;
import com.shop.util.BeanCopyUtils;
import com.shop.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class UserAddressService {

    @Autowired
    private UserAddressDao userAddressDao;
    @Autowired
    private UserAddressSQLDao userAddressSQLDao;

    public BaseResponse findSingle(Long id) {
        UserAddress userAddress = userAddressDao.findSingle(id);
        return new BaseResponse(userAddress);
    }

    public BaseResponse findDefault(Long userId) {
        UserAddress userAddress = userAddressDao.findDefault(userId);
        if(null != userAddress) {
            return new BaseResponse(userAddress);
        }

        List<UserAddress> list = userAddressDao.findByUid(userId);
        if(null != list && list.size() > 0) {
            return new BaseResponse(list.get(0));
        }

        return new BaseResponse();
    }

    public BaseResponse page(Long userId) {
        Page<UserAddress> page = userAddressSQLDao.page(userId);
        return new BaseResponse(page);
    }

    public BaseResponse add(Long userId, UserAddressReq req) {
        UserAddress userAddress = new UserAddress();

        try {
            // 字段赋值
            BeanCopyUtils.copyProperties(userAddress, req);
        } catch (Exception e) {
            log.error("添加用户地址赋值异常！", e);
            return BaseResponse.FAIL;
        }

        // 是否设置为默认
        if(TrueFalseEnum.TRUE.getCode() == req.getIsDefault()) {    // 是
            // 修改该用户默认收货地址为非默认
            userAddressDao.updateNotDefault(userId);
        }

        // 保存入库
        userAddress.setUid(userId);
        userAddressDao.save(userAddress);

        return new BaseResponse(userAddress);
    }

    public BaseResponse edit(Long id, UserAddressReq req) {
        UserAddress userAddress = userAddressDao.findSingle(id);
        if(null == userAddress) {
            return BaseResponse.fail("该收货地址信息不存在!");
        }

        // 是否设置为默认
        if(TrueFalseEnum.TRUE.getCode() == req.getIsDefault()) {    // 是
            // 当前是否为非默认
            if(TrueFalseEnum.TRUE.getCode() != userAddress.getIsDefault()) {
                // 修改该用户默认收货地址为非默认
                userAddressDao.updateNotDefault(userAddress.getUid());
            }
        }

        try {
            // 字段赋值
            BeanCopyUtils.copyProperties(userAddress, req);
        } catch (Exception e) {
            log.error("添加用户地址赋值异常！", e);
            return BaseResponse.FAIL;
        }

        // 保存入库
        userAddressDao.save(userAddress);

        return new BaseResponse(userAddress);
    }

    public BaseResponse del(Long id) {
        UserAddress userAddress = userAddressDao.findSingle(id);
        if(null == userAddress) {
            return BaseResponse.fail("该收货地址信息不存在!");
        }

        // 删除
        userAddressDao.delete(userAddress);

        return new BaseResponse();
    }
}
